package com.changcheng.project.apppublish.controller;

import java.util.List;

import com.changcheng.common.TextUtils;
import com.changcheng.project.applist.domain.AAppfileList;
import com.changcheng.project.applist.service.IAAppfileListService;
import com.changcheng.project.apppublishlog.domain.AAppPublishLog;
import com.changcheng.project.apppublishlog.service.IAAppPublishLogService;
import com.changcheng.project.obupublishlog.domain.AObuPublishLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.changcheng.framework.aspectj.lang.annotation.Log;
import com.changcheng.framework.aspectj.lang.enums.BusinessType;
import com.changcheng.project.apppublish.domain.AAppPublish;
import com.changcheng.project.apppublish.service.IAAppPublishService;
import com.changcheng.framework.web.controller.BaseController;
import com.changcheng.framework.web.domain.AjaxResult;
import com.changcheng.common.utils.poi.ExcelUtil;
import com.changcheng.framework.web.page.TableDataInfo;

/**
 * APP发布Controller
 *
 * @author dazhi
 * @date 2020-10-31
 */
@Controller
@RequestMapping("/apppublish/apppublish")
public class AAppPublishController extends BaseController {
    private String prefix = "apppublish/apppublish";

    @Autowired
    private IAAppfileListService aAppfileListService;
    @Autowired
    private IAAppPublishService aAppPublishService;
    @Autowired
    private IAAppPublishLogService aAppPublishLogService;



    @RequiresPermissions("apppublish:apppublish:view")
    @GetMapping()
    public String apppublish() {
        return prefix + "/apppublish";
    }

    /**
     * 查询APP发布列表
     */
    @RequiresPermissions("apppublish:apppublish:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AAppPublish aAppPublish) {
        startPage();
        List<AAppPublish> list = aAppPublishService.selectAAppPublishList(aAppPublish);
        return getDataTable(list);
    }

    /**
     * 导出APP发布列表
     */
    @RequiresPermissions("apppublish:apppublish:export")
    @Log(title = "APP发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AAppPublish aAppPublish) {
        List<AAppPublish> list = aAppPublishService.selectAAppPublishList(aAppPublish);
        ExcelUtil<AAppPublish> util = new ExcelUtil<AAppPublish>(AAppPublish.class);
        return util.exportExcel(list, "apppublish");
    }

    /**
     * 新增APP发布
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    int zhi;

    /**
     * 新增保存APP发布
     */
    @RequiresPermissions("apppublish:apppublish:add")
    @Log(title = "APP发布", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AAppPublish aAppPublish) {
        if (!TextUtils.isBlank(aAppPublish.getAppPublishName()) &&
                !TextUtils.isBlank(aAppPublish.getAppPublishVersion()) &&
                !TextUtils.isBlank(aAppPublish.getAppPublishTime()) &&
                !TextUtils.isBlank(aAppPublish.getAppPublishUrl()) &&
                !TextUtils.isBlank(aAppPublish.getAppPublishPeoplename()) &&
                !TextUtils.isBlank(aAppPublish.getAppPublishLiang()) &&
                !TextUtils.isBlank(aAppPublish.getStationName()) &&
                !TextUtils.isBlank(aAppPublish.getStationCode())) {

            List<AAppPublish> list = aAppPublishService.selectAAppPublishList(null);
            if (list != null && list.size() > 0) {
                zhi = -1;
                return toAjax2(zhi, "只能存在一条发布记录，请删除原有记录后添加！");
            } else {
                //加入发布
                zhi = aAppPublishService.insertAAppPublish(aAppPublish);
                //加入记录
                AAppPublishLog AObuPublishLogs = new AAppPublishLog();
                AObuPublishLogs.setAppPublishLiang(aAppPublish.getAppPublishLiang());
                AObuPublishLogs.setAppPublishName(aAppPublish.getAppPublishName());
                AObuPublishLogs.setAppPublishTime(aAppPublish.getAppPublishTime());
                AObuPublishLogs.setAppPublishUrl(aAppPublish.getAppPublishUrl());
                AObuPublishLogs.setAppPublishVersion(aAppPublish.getAppPublishVersion());
                AObuPublishLogs.setStationName(aAppPublish.getStationName());
                AObuPublishLogs.setStationCode(aAppPublish.getStationCode());
                AObuPublishLogs.setAppPublishPeoplename(aAppPublish.getAppPublishPeoplename());
                aAppPublishLogService.insertAAppPublishLog(AObuPublishLogs);
                return toAjax(zhi);
            }
        } else {
            zhi = -1;
            return toAjax2(zhi, "数据不能为空！");
        }


    }

    /**
     * 修改APP发布
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        AAppPublish aAppPublish = aAppPublishService.selectAAppPublishById(id);
        mmap.put("aAppPublish", aAppPublish);
        return prefix + "/edit";
    }

    /**
     * 修改保存APP发布
     */
    @RequiresPermissions("apppublish:apppublish:edit")
    @Log(title = "APP发布", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AAppPublish aAppPublish) {
        return toAjax(aAppPublishService.updateAAppPublish(aAppPublish));
    }

    /**
     * 删除APP发布
     */
    @RequiresPermissions("apppublish:apppublish:remove")
    @Log(title = "APP发布", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(aAppPublishService.deleteAAppPublishByIds(ids));
    }


    /**
     * 查询APP文件列表列表
     */

    @PostMapping("/applist")
    @ResponseBody
    public TableDataInfo list(AAppfileList aAppfileList) {
        startPage();
        List<AAppfileList> list = aAppfileListService.selectAAppfileListList(aAppfileList);
        return getDataTable(list);
    }

}
